
# setup -------------------------------------------------------------------

pacman::p_load(tidyverse, janitor, ggdist, modelsummary, ragg)

# Create Coefplot ---------------------------------------------------------

# fit
models <- list(
    "regular FE" = fd_mod_statese_ls[[4]],
    "varying slopes" = fd_mod_statese_ls[[6]]
    )

# summarize
dat <- map_dfr(c(.9, .95), function(x) {
    modelplot(models, conf_level = x, draw = FALSE) %>%
        mutate(.width = x,
               term = case_when(term == "parl_treat_fct::Exit" ~ "Greens Exit",
                                term == "parl_treat_fct::Entry" ~ "Greens Entry",
                                T ~ as.character(term))) 
    }
    )

# plot entry
plot_1 <- dat %>% 
    filter(term %in% c("Greens Entry")) %>% 
    mutate(
        term = factor(term),
        term = fct_relevel(term, "Greens Entry", after = Inf)) %>% 
    ggplot(., aes(
    y = term, x = estimate,
    xmin = conf.low, xmax = conf.high,
    colour = model)) +
    ggdist::geom_pointinterval(
        position = position_dodge(width = 0.25),
        interval_size_range = c(1.5, 2.5),
        fatten_point = .9,
        point_fill = "white") +
    xlim(-1, 2.5) +
    geom_vline(xintercept = 0, linetype = "dashed") +
    scale_color_grey() + 
    xlab("Effect of Green entry into state parliament on change of CDU/CSU vote shares.") +
    ylab("") +
    theme_hanno() + 
    theme(
        axis.text = element_text(size = 12),
        axis.title = element_text(size = 14),
        legend.title = element_blank(),
        legend.position = c(0.2, 0.02),
        legend.justification = c("right", "bottom"),
        legend.box.just = "right",
        legend.margin = margin(6, 6, 6, 6)
    )
plot_1
ggsave(
    plot = plot_1,
    width = 10,
    height = 3,
    device = "png",
    dpi = 600,
    filename = "output/figures/fig1.png"
)

# 
# # plot entry
# plot_2 <- dat %>% 
#     filter(term %in% c("Greens Exit")) %>% 
#     mutate(
#         term = factor(term),
#         term = fct_relevel(term, "Greens Exit", after = Inf)) %>% 
#     ggplot(., aes(
#         y = term, x = estimate,
#         xmin = conf.low, xmax = conf.high,
#         colour = model)) +
#     ggdist::geom_pointinterval(
#         position = position_dodge(width = 0.25),
#         interval_size_range = c(1.5, 2.5),
#         fatten_point = .9,
#         point_fill = "white") +
#     xlim(-4, 1) +
#     geom_vline(xintercept = 0, linetype = "dashed") +
#     scale_color_grey() + 
#     xlab("Effect of Green exit from state parliament on change of CDU/CSU vote shares.") +
#     ylab("") +
#     theme_hanno() + 
#     theme(
#         axis.text = element_text(size = 12),
#         axis.title = element_text(size = 14),
#         legend.title = element_blank(),
#         legend.position = c(0.99, 0.01),
#         legend.justification = c("right", "bottom"),
#         legend.box.just = "right",
#         legend.margin = margin(6, 6, 6, 6)
#     )
# plot_2
# ggsave(
#     plot = plot_2,
#     width = 10,
#     height = 3,
#     device = "pdf",
#     filename = "output/figures/main_exit_fd_plots.pdf"
# )
# 
